package cn.wp.ewallet.wmps.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.wp.common.core.authentication.AuthenticationContext;
import cn.wp.common.core.model.WpPage;
import cn.wp.common.core.util.BeanUtil;
import cn.wp.ewallet.common.model.dto.MoneyTypeInfoDto;
import cn.wp.ewallet.common.service.MoneyTypeInfoService;
import cn.wp.ewallet.wallet.dao.AccountInfoDao;
import cn.wp.ewallet.wallet.dao.UserWalletRelDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.extension.util.EwalletWalletUtil;
import cn.wp.ewallet.wallet.model.entity.UserWalletRelDo;
import cn.wp.ewallet.wmps.dao.EarnDetailLogDao;
import cn.wp.ewallet.wmps.model.dto.EarnDetailLogDto;
import cn.wp.ewallet.wmps.model.entity.EarnDetailLogDoo;
import cn.wp.ewallet.wmps.service.EarnDetailLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangjin on 2020/1/9
 */
@Service
public class EarnDetailLogServiceImpl implements EarnDetailLogService {

    @Autowired
    private EarnDetailLogDao earnDetailLogDao;

    @Autowired
    private AuthenticationContext authenticationContext;

    @Autowired
    private UserWalletRelDao userWalletRelDao;

    @Autowired
    private MoneyTypeInfoService moneyTypeInfoService;

    @Override
    public WpPage<EarnDetailLogDto> getEarnDetailLogList(int month, WpPage page, String earnStatus) {
        String userId = authenticationContext.currentUser().getId();
        UserWalletRelDo userWalletRelDo = userWalletRelDao.selectUserWalletRel(userId);
        String walletAddress = userWalletRelDo.getWalletAddress();
        WpPage<EarnDetailLogDoo> wpPage = earnDetailLogDao.selectEarnDetailLogList(month, page, earnStatus,walletAddress);

        //查询SHQC数据精度
        MoneyTypeInfoDto moneyTypeInfoDto = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_SHQC);
        BigDecimal unit = BigDecimal.valueOf(Double.valueOf(moneyTypeInfoDto.getMoneyTypeUnit()));
        List<EarnDetailLogDoo> list = wpPage.getList();
        //数据精度转换
        if (CollectionUtil.isNotEmpty(list)) {
            for (EarnDetailLogDoo doo : list) {
                if (null != doo.getDailyEarn()) {
                    doo.setDailyEarn(EwalletWalletUtil.toUpperWei(doo.getDailyEarn(), unit));
                }
            }
        }
        return wpPage.doConvertDto(EarnDetailLogDto.class);
    }
}
